package com.test.demo.mybatisplus;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.plugins.pagination.optimize.JsqlParserCountOptimize;
import com.test.demo.mybatisplus.entity.xxl.XxlJobLog;
import com.test.demo.mybatisplus.mapper.dynamic.GatewayRoutesMapper;
import com.test.demo.mybatisplus.mapper.xxl.XxlJobLogMapper;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author laigl
 * @ClassName
 * @Description
 * @Date 2020/03/04 17:14
 */
@RestController
@SpringBootApplication
@MapperScan("com.test.demo.mybatisplus")
public class App {

    public static void main(String[] args) {
        SpringApplication.run(App.class, args);
    }

    /**
     * 分页插件生效
     */
    @Bean
    public PaginationInterceptor paginationInterceptor() {
        // 开启 count 的 join 优化,只针对 left join !!!
        return new PaginationInterceptor().setCountSqlParser(new JsqlParserCountOptimize(true));
    }

    @Autowired
    private XxlJobLogMapper xxlJobLogMapper;
    @Autowired
    private GatewayRoutesMapper gatewayRoutesMapper;

    @GetMapping("test1")
    public String test1() {
        System.out.println(xxlJobLogMapper.selectById(1));
        System.out.println(gatewayRoutesMapper.selectById(1));
        Page<XxlJobLog> page = new Page<>(2, 2);
        QueryWrapper<XxlJobLog> queryWrapper = new QueryWrapper<>();
        IPage<XxlJobLog> ipage = xxlJobLogMapper.selectPage(page, queryWrapper);
        ipage.getRecords().forEach(a -> System.out.println(a));
        return ipage.getTotal() + "";
    }

}
